#include<string>
#include<vector>
#include<iostream>
#include<unordered_map>
using std::unordered_map;
using std::string;
using std::vector;


class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int m = p.size(), n = s.size();
        if (m > n) return {0};
        
        vector<int> differ(26, 0);
        int diffCount = 0;
        for (int i = 0; i < m; i++) {
            differ[p[i]-'a']++;
            differ[s[i]-'a']--;
        }
        for (int i = 0; i < 26; i++)
            if (differ[i] != 0) diffCount++;
        
        vector<int> ans;
        if (diffCount == 0) ans.push_back(0);
        
        for (int k = 1; k <= n-m; k++) {
            int prevIdx = s[k-1]-'a';
            differ[prevIdx]++;
            if (differ[prevIdx] == 0) diffCount--;
            else if (differ[prevIdx] == 1) diffCount++;
            differ[nextIdx]--;
            if (differ[nextIdx] == 0) diffCount--;
            else if (differ[nextIdx] == -1) diffCount++;
            if (diffCount == 0) ans.push_back(k);
        }
        return ans;
    }

};